Forex data Pandas change timezone

by: karunswaroop, 8 years ago


How do we change timezone in Pandas DataFrame?

Wrote simple code to read 1min EURUSD data (DateTime/Open/High/Low/Close/Vol), the sample data is in EST timezone, I need to convert it to UTC.

df.tz_convert(pytz.timezone('UTC'))


is failing. with error "TypeError: Cannot convert tz-naive timestamps, use tz_localize to localize"

sample data from DAT_ASCII_EURUSD_M1_SmallSample.csv file
---- start -----

20160103 170000;1.087010;1.087130;1.087010;1.087130;0
20160103 170100;1.087120;1.087120;1.087120;1.087120;0
20160103 170200;1.087080;1.087220;1.087080;1.087220;0
20160104 000100;1.087830;1.087840;1.087640;1.087640;0
20160104 000200;1.087640;1.088220;1.087640;1.088220;0
20160104 000300;1.088220;1.088220;1.088040;1.088050;0
20160105 000000;1.082270;1.082270;1.082160;1.082160;0
20160105 000100;1.082160;1.082160;1.082130;1.082140;0
20160105 000200;1.082150;1.082240;1.082150;1.082240;0

sample data ---- end ------

    import pandas as pd
    import pytz
    
    filename = "DAT_ASCII_EURUSD_M1_SmallSample.csv"
    
    df = pd.read_csv(filename, sep=";", names=['DateTime','Open','High','Low','Close','Vol'],
                     parse_dates = [0], index_col = 'DateTime')
    
    df.tz_localize(pytz.timezone('US/Eastern'))
    df.tz_convert(pytz.timezone('UTC'))
    
    print(df)





You must be logged in to post. Please login or register an account.



Found it, I need to use

df = df.tz_localize(pytz.timezone('US/Eastern'))
df = df.tz_convert(pytz.timezone('UTC'))

since tz_localize is not an in-place operation, but instead returns a new DataFrame.

-karunswaroop 8 years ago

You must be logged in to post. Please login or register an account.